// Copyright 2019 The rhyme_lph Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// u [author] rhyme_lph
// d [email]  rhymelph@gmail.com
// W [github] https://github.com/rhymelph

import 'package:base_plugin/base_plugin.dart';
import 'package:finance_app/generated/a.dart';
import 'package:finance_app/generated/l10n.dart';
import 'package:finance_app/src/page/dashboard/products_services/views/products_view.dart';
import 'package:finance_app/src/page/dashboard/products_services/views/services_view.dart';
import 'package:finance_app/src/provider/dashboard/products_services/products_and_services_provider.dart';
import 'package:finance_app/src/router_gen/router_meta.dart';
import 'package:finance_app/src/widgets/button/assistant_button.dart';
import 'package:finance_app/src/widgets/button/normal_float_svg_button.dart';
import 'package:finance_app/src/widgets/button/normal_popup_button.dart';
import 'package:finance_app/src/widgets/common_app_bar.dart';
import 'package:finance_app/src/widgets/common_tab_bar.dart';
import 'package:flutter/material.dart';

@RouterMeta(paramName: "productsAndServicesPage")
class ProductsAndServicesPage extends StatefulWidget {
  final bool isSelectMode;
  final int initIndex;
  final String initStatus;
  final bool isShowZero;
  final bool usePurchasePrice;

  const ProductsAndServicesPage({Key key, this.isSelectMode, this.isShowZero, this.initIndex, this.initStatus, this.usePurchasePrice}) : super(key: key);

  @override
  _ProductsAndServicesPageState createState() => _ProductsAndServicesPageState();
}

class _ProductsAndServicesPageState extends State<ProductsAndServicesPage>
    with PagePresenter<ProductsAndServicesPage, ProductsAndServicesProvider>, TickerProviderStateMixin {
  TabController tabController;

  @override
  PreferredSizeWidget get appBar => getDefaultAppBar(
      title: S.current.products__services,
      actions: widget.isSelectMode != true
          ? [
              AssistantButton(
                paddingRight: 0.0,
                entryPoint: 'Products & Services',
              ),
              listBuilder<List<String>>(
                selector: (_, __) => List.from(__.menuList),
                builder: (BuildContext context, List<String> value, Widget child) => NormalMorePopupButton(
                  horizontalPadding: getDp(10),
                  menuList: provider.menuList,
                  onSelect: provider.onSelectMenu,
                ),
              )
            ]
          : []);

  @override
  void initState() {
    if (widget.initIndex != null && widget.initIndex == 1) {
      provider.isService = true;
    }
    if (widget.usePurchasePrice != null) provider.usePurchasePrice = widget.usePurchasePrice;

    super.initState();
    tabController = TabController(length: 2, vsync: this, initialIndex: widget.initIndex ?? 0)
      ..addListener(() {
        provider.onChange(tabController);
      });
    provider.tabController = tabController;
  }

  @override
  void dispose() {
    super.dispose();
    tabController.dispose();
  }

  @override
  Widget get buildWidget => listBuilder<bool>(
        selector: (_, __) => __.buttonBarIsUp,
        builder: (BuildContext context, bool value, Widget child) => Stack(
          children: [
            child,
            if (widget.isSelectMode != true) _buildBottomButton(value),
            // _buildTop(),
          ],
        ),
        child: _buildRecordList(),
      );

  Widget _buildRecordList() => ColoredBox(
        color: Colors.white,
        child: Column(
          children: [
            CommonTabBar(
              controller: tabController,
              tabs: [
                MyTab(
                  text: S.current.products,
                ),
                MyTab(
                  text: S.current.services,
                ),
              ],
            ),
            Expanded(
                child: TabBarView(controller: tabController, children: [
              ProductsView(
                isSelectMode: widget.isSelectMode,
                initStatus: widget.initIndex == 0 ? widget.initStatus : null,
                isShowZero: widget.isShowZero,
              ),
              ServicesView(
                isSelectMode: widget.isSelectMode,
                initStatus: widget.initIndex == 1 ? widget.initStatus : null,
              ),
            ])),
          ],
        ),
      );

  Widget _buildBottomButton(bool isTop) => AnimatedPositioned(
      duration: Duration(milliseconds: 300),
      left: 0.0,
      right: 0.0,
      bottom: isTop ? MediaQuery.of(context).padding.bottom + getDp(40) : -getDp(60),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          NormalFloatSvgButton(
            onTap: provider.onNavAdd,
            asset: A.assetsImagesDashboardAdd,
          ),
        ],
      ));
}
